﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Small.Html.Enum;

namespace Small.Html.Options
{
    public class JqGridOptions:JqBase
    {
        List<string> list = new List<string>();

        private string url;

        public string Url
        {
            get {
                
                return url; }
            set { url = value;
            list.Add(IsStringNull("url", url));
            }
        }
        private string height = "150";

        public string Height
        {
            get {
               
               
                return height; }
            set { height = value;
            list.Add(IsStringNull("height", height));
            }
        }
        private Int32 page = 1;

        public Int32 Page
        {
            get { return page; }
            set { page = value;
            list.Add(IsInteger("page", page));
            }
        }
        private Int32 rowNum = 20;

        public Int32 RowNum
        {
            get { return rowNum; }
            set { rowNum = value;
            list.Add(IsInteger("rowNum", rowNum));
            }
        }
        private int rowTotal = 0;

        public int RowTotal
        {
            get { return rowTotal; }
            set { rowTotal = value;
            list.Add(IsInteger("rowTotal", rowTotal));
            }
        }
        private Int32 records = 0;

        public Int32 Records
        {
            get { return records; }
            set { records = value;
            list.Add(IsInteger("records", records));
            }
        }
        private string pager = string.Empty;

        public string Pager
        {
            get { return pager; }
            set { pager = value;

            list.Add(IsStringNull("pager", pager));
            }
        }
        private bool pgbuttons = true;

        public bool Pgbuttons
        {
            get { return pgbuttons; }
            set { pgbuttons = value;
            list.Add(IsBool("pgbuttons", pgbuttons));
            }
        }
        private bool pginput = true;

        public bool Pginput
        {
            get { return pginput; }
            set { pginput = value;
            list.Add(IsBool("pginput", pginput));
            }
        }
        private JqGridColModel[] colModel;

        public JqGridColModel[] ColModel
        {
            get { return colModel; }
            set { colModel = value;

            list.Add(GetColModel());
            }
        }
        private Int32[] rowList;

        public Int32[] RowList
        {
            get { return rowList; }
            set { rowList = value;

            var rowList1 = new List<string>();
            foreach (var item in rowList)
            {
                rowList1.Add(Convert.ToString(item));
                
            }
            list.Add("rowList:["+String.Join(",",rowList1)+"]");
            }
        }
        private String[] colNames;

        public String[] ColNames
        {
            get { return colNames; }
            set { colNames = value;
            list.Add(GetColNames());
            
            }
        }
        private OrderByType sortorder = OrderByType.asc;

        public OrderByType Sortorder
        {
            get { return sortorder; }
            set { sortorder = value;
            list.Add(IsStringNull("sortorder", sortorder.ToString()));
            
            }
        }
        private string sortname = string.Empty;

        public string Sortname
        {
            get { return sortname; }
            set { sortname = value;
            list.Add(IsStringNull("sortname", sortname));
            }
        }
        private DataType datatype = DataType.json;

        public DataType Datatype
        {
            get { return datatype; }
            set { datatype = value;
           
            }
        }
        private FormMethodType mtype = FormMethodType.get;

        public FormMethodType Mtype
        {
            get { return mtype; }
            set { mtype = value;
          
            }
        }
        private bool altRows = false;

        public bool AltRows
        {
            get { return altRows; }
            set { altRows = value;

            list.Add(IsBool("altRows", altRows));
            }
        }
        private String[] selarrrow;

        public String[] Selarrrow
        {
            get { return selarrrow; }
            set { selarrrow = value;
            list.Add(IsStringNull("selarrrow", String.Join(",",selarrrow)));
            }
        }
        private String[] savedRow;

        public String[] SavedRow
        {
            get { return savedRow; }
            set { savedRow = value; 
            list.Add(IsStringNull("savedRow", String.Join(",",savedRow)));
            }
        }
        private bool shrinkToFit = true;

        public bool ShrinkToFit
        {
            get { return shrinkToFit; }
            set { shrinkToFit = value;

            list.Add(IsBool("ShrinkToFit",ShrinkToFit));
            }
        }

        private JqXmlReader xmlReader;

        public JqXmlReader XmlReader
        {
            get { return xmlReader; }
            set { xmlReader = value;
            list.Add(IsStringNull("xmlReader", xmlReader.GenerateString()));
            }
        }
        private JqJsonReader jsonReader;

        public JqJsonReader JsonReader
        {
            get { return jsonReader; }
            set { jsonReader = value;
            list.Add(IsStringNull("jsonReader", jsonReader.GenerateString()));
            }
        }

        private bool subGrid = false;

        public bool SubGrid
        {
            get { return subGrid; }
            set { subGrid = value;
            list.Add(IsBool("subGrid", subGrid));
            }
        }
        private string subGridUrl;

        public string SubGridUrl
        {
            get { return subGridUrl; }
            set { subGridUrl = value;
            list.Add(IsStringNull("subGridUrl", subGridUrl));
            }
        }
        private JqSubGridModel subGridModel;

        public JqSubGridModel SubGridModel
        {
            get { return subGridModel; }
            set { subGridModel = value; 
             list.Add(subGridModel.GenerateString());
            }
        }


        private Int32 reccount = 0;

        public Int32 Reccount
        {
            get { return reccount; }
            set { reccount = value;

            list.Add(IsInteger("reccount", reccount));
            }
        }
        private Int32 lastpage = 0;

        public Int32 Lastpage
        {
            get { return lastpage; }
            set { lastpage = value;
            list.Add(IsInteger("lastpage", lastpage));
            }
        }
        private Int32 lastsort = 0;

        public Int32 Lastsort
        {
            get { return lastsort; }
            set { lastsort = value;
            list.Add(IsInteger("Lastsort", Lastsort));
            }
        }
        private string selrow = null;

        public string Selrow
        {
            get { return selrow; }
            set { selrow = value;
            list.Add(IsStringNull("selrow", selrow));
            }
        }


        private bool viewrecords = false;

        public bool Viewrecords
        {
            get { return viewrecords; }
            set { viewrecords = value;
            list.Add(IsBool("viewrecords", viewrecords));
            }
        }
        private bool loadonce = false;

        public bool Loadonce
        {
            get { return loadonce; }
            set { loadonce = value;
            list.Add(IsBool("loadonce", loadonce));
            }
        }
        private bool multiselect = false;

        public bool Multiselect
        {
            get { return multiselect; }
            set { multiselect = value;

            list.Add(IsBool("multiselect", multiselect));
            }
        }
        private bool multikey = false;

        public bool Multikey
        {
            get { return multikey; }
            set { multikey = value;
            list.Add(IsBool("multikey", multikey));
            }
        }
        private string editurl = null;

        public string Editurl
        {
            get { return editurl; }
            set { editurl = value;
            list.Add(IsStringNull("editurl", editurl));
            }
        }
        private bool search = false;

        public bool Search
        {
            get { return search; }
            set { search = value;
            list.Add(IsBool("search", search));
            }
        }
        private string caption = "";

        public string Caption
        {
            get { return caption; }
            set { caption = value;
            list.Add(IsStringNull("caption", caption));
            }
        }
        private bool hidegrid = true;

        public bool Hidegrid
        {
            get { return hidegrid; }
            set { hidegrid = value;

            list.Add(IsBool("hidegrid", hidegrid));
            }
        }
        private bool hiddengrid = false;

        public bool Hiddengrid
        {
            get { return hiddengrid; }
            set { hiddengrid = value;
            list.Add(IsBool("hiddengrid", hiddengrid));
            }
        }
        private string postData;

        public string PostData
        {
            get { return postData; }
            set { postData = value;
            list.Add(IsStringNull("postData", postData));
            }
        }
        private string userData;

        public string UserData
        {
            get { return userData; }
            set { userData = value;
            list.Add(IsStringNull("userData", userData));
            }
        }
        private bool treeGrid = false;

        public bool TreeGrid
        {
            get { return treeGrid; }
            set { treeGrid = value;
            list.Add(IsBool("treeGrid", treeGrid));
            }
        }
        private string treeGridModel = "nested";

        public string TreeGridModel
        {
            get { return treeGridModel; }
            set { treeGridModel = value;
            list.Add(IsStringNull("nested", treeGridModel));
            }
        }
        private JqTreeReader treeReader;

        public JqTreeReader TreeReader
        {
            get { return treeReader; }
            set { treeReader = value;
            list.Add(treeReader.GenerateString());
            }
        }
        private Int32 treeANode;

        public Int32 TreeANode
        {
            get { return treeANode; }
            set { treeANode = value;
            list.Add(IsInteger("treeANode", treeANode));
            }
        }
        private string ExpandColumn;

        public string ExpandColumn1
        {
            get { return ExpandColumn; }
            set { ExpandColumn = value;
            list.Add(IsStringNull("ExpandColumn", ExpandColumn));
            }
        }
        private Int32 tree_root_level = 0;

        public Int32 Tree_root_level
        {
            get { return tree_root_level; }
            set { tree_root_level = value;
            list.Add(IsInteger("tree_root_level", tree_root_level));
            }
        }
        private string prmNames;
        //= {page="page";rows="rows"; sort= "sidx";order= "sord"; search="_search"; nd="nd"; id="id";oper="oper";editoper="edit";addoper="add";deloper="del"; subgridid="id"; npage= null; totalrows="totalrows"};
        private bool forceFit = false;

        public bool ForceFit
        {
            get { return forceFit; }
            set { forceFit = value;
            list.Add(IsBool("forceFit", forceFit));
            }
        }
        private string gridstate = "visible";

        public string Gridstate
        {
            get { return gridstate; }
            set { gridstate = value;
            list.Add(IsStringNull("gridstate", gridstate));
            }
        }
        private bool cellEdit = false;

        public bool CellEdit
        {
            get { return cellEdit; }
            set { cellEdit = value;
            list.Add(IsBool("cellEdit", cellEdit));
            }
        }
        private JqCellSubmitType cellsubmit;

        public JqCellSubmitType Cellsubmit
        {
            get { return cellsubmit; }
            set { cellsubmit = value;
            list.Add(IsStringNull("cellsubmit", cellsubmit.ToString()));
            }
        }
        private Int32 nv = 0;

        public Int32 Nv
        {
            get { return nv; }
            set { nv = value;
            list.Add(IsInteger("Nv", Nv));
            }
        }
        private string loadui = "enable";

        public string Loadui
        {
            get { return loadui; }
            set { loadui = value;
            list.Add(IsStringNull("loadui", loadui));
            }
        }
        private String toolbar = "[false;\"\"]";

        public String Toolbar
        {
            get { return toolbar; }
            set { toolbar = value;
            list.Add(IsStringNull("toolbar", toolbar));
            }
        }
        private bool scroll = false;

        public bool Scroll
        {
            get { return scroll; }
            set { scroll = value;
            list.Add(IsBool("Scroll", Scroll));
            }
        }
        private bool multiboxonly = false;

        public bool Multiboxonly
        {
            get { return multiboxonly; }
            set { multiboxonly = value;
            list.Add(IsBool("multiboxonly", multiboxonly));
            }
        }
        private bool deselectAfterSort = true;

        public bool DeselectAfterSort
        {
            get { return deselectAfterSort; }
            set { deselectAfterSort = value;
            list.Add(IsBool("deselectAfterSort", deselectAfterSort));
            }
        }
        private bool scrollrows = false;

        public bool Scrollrows
        {
            get { return scrollrows; }
            set { scrollrows = value;
            list.Add(IsBool("scrollrows", scrollrows));
            }
        }
        private bool autowidth = false;

        public bool Autowidth
        {
            get { return autowidth; }
            set { autowidth = value;
            list.Add(IsBool("autowidth", autowidth));
            }
        }
        private Int32 scrollOffset;

        public Int32 ScrollOffset
        {
            get { return scrollOffset; }
            set { scrollOffset = value;
            list.Add(IsInteger("scrollOffset", scrollOffset));
            }
        }
        private Int32 cellLayout;

        public Int32 CellLayout
        {
            get { return cellLayout; }
            set { cellLayout = value;
            list.Add(IsInteger("cellLayout", cellLayout));
            }
        }
        private string subGridWidth;

        public string SubGridWidth
        {
            get { return subGridWidth; }
            set { subGridWidth = value;
            list.Add(IsStringNull("subGridWidth", subGridWidth));
            }
        }
        private Int32 multiselectWidth = 20;

        public Int32 MultiselectWidth
        {
            get { return multiselectWidth; }
            set { multiselectWidth = value;
            list.Add(IsInteger("multiselectWidth", multiselectWidth));
            }
        }
        private bool gridview = false;

        public bool Gridview
        {
            get { return gridview; }
            set { gridview = value;
            list.Add(IsBool("gridview", gridview));
            }
        }
        private Int32 rownumWidth = 25;

        public Int32 RownumWidth
        {
            get { return rownumWidth; }
            set { rownumWidth = value;
            list.Add(IsInteger("rownumWidth", rownumWidth));
            }
        }
        private bool rownumbers = false;

        public bool Rownumbers
        {
            get { return rownumbers; }
            set { rownumbers = value;
            list.Add(IsBool("rownumbers", rownumbers));
            }
        }
        private TextAlign pagerpos;

        public TextAlign Pagerpos
        {
            get { return pagerpos; }
            set { pagerpos = value;
            list.Add(IsStringNull("pagerpos", pagerpos.ToString()));
            }
        }
        private TextAlign recordpos;

        public TextAlign Recordpos
        {
            get { return recordpos; }
            set { recordpos = value;
            list.Add(IsStringNull("recordpos", recordpos.ToString()));
            }
        }
        private bool footerrow = false;

        public bool Footerrow
        {
            get { return footerrow; }
            set { footerrow = value;
            list.Add(IsBool("footerrow", footerrow));
            }
        }
        private bool userDataOnFooter = false;

        public bool UserDataOnFooter
        {
            get { return userDataOnFooter; }
            set { userDataOnFooter = value;
            list.Add(IsBool("userDataOnFooter", userDataOnFooter));
            }
        }
        private bool hoverrows = true;

        public bool Hoverrows
        {
            get { return hoverrows; }
            set { hoverrows = value;
            list.Add(IsBool("hoverrows", hoverrows));
            }
        }
        private string altclass = "ui-priority-secondary";

        public string Altclass
        {
            get { return altclass; }
            set { altclass = value;
            list.Add(IsStringNull("altclass", altclass));
            }
        }
        private string viewsortcols = "[false;\"vertical\";true]";

        public string Viewsortcols
        {
            get { return viewsortcols; }
            set { viewsortcols = value;
            list.Add(IsStringNull("viewsortcols", viewsortcols));
            }
        }
        private string resizeclass = "";

        public string Resizeclass
        {
            get { return resizeclass; }
            set { resizeclass = value;
            list.Add(IsStringNull("resizeclass", resizeclass));
            }
        }
        private bool autoencode = false;

        public bool Autoencode
        {
            get { return autoencode; }
            set { autoencode = value;
            list.Add(IsBool("autoencode", autoencode));
            }
        }
        private string[] remapColumns;

        public string[] RemapColumns
        {
            get { return remapColumns; }
            set { remapColumns = value;

             var remap=" remapColumns:["+string.Join(",",remapColumns)+"] ";
             list.Add(IsStringNull("remapColumns", remap));
            }
        }
        private JqAjaxGridOptions ajaxGridOptions;//={};

        public JqAjaxGridOptions AjaxGridOptions
        {
            get { return ajaxGridOptions; }
            set { ajaxGridOptions = value;
            list.Add(AjaxGridOptions.GenerateString());
            }
        }
        private string direction = "ltr";

        public string Direction
        {
            get { return direction; }
            set { direction = value;
            list.Add(IsStringNull("direction", direction));
            }
        }
        private bool toppager = false;

        public bool Toppager
        {
            get { return toppager; }
            set { toppager = value;
            list.Add(IsBool("toppager", toppager));
            }
        }
        private bool headertitles = false;

        public bool Headertitles
        {
            get { return headertitles; }
            set { headertitles = value;
            list.Add(IsBool("headertitles", headertitles));
            }
        }
        private Int32 scrollTimeout = 40;

        public Int32 ScrollTimeout
        {
            get { return scrollTimeout; }
            set { scrollTimeout = value;
            list.Add(IsInteger("scrollTimeout", scrollTimeout));
            }
        }
        private string data = "";

        public string Data
        {
            get { return data; }
            set { data = value;
            list.Add(IsStringNull("data", data));
            }
        }
        private JqIndex _index;

        public JqIndex Index
        {
            get { return _index; }
            set { _index = value;
            list.Add(_index.GenerateString());
            }
        }
        private bool grouping = false;

        public bool Grouping
        {
            get { return grouping; }
            set { grouping = value;
            list.Add(IsBool("grouping", grouping));
            }
        }
        private JqGroupingView groupingView;

        public JqGroupingView GroupingView
        {
            get { return groupingView; }
            set { groupingView = value;
            list.Add(groupingView.GenerateString());
            
            }
        }

        private JqGridEvent envents;

        public JqGridEvent Envents
        {
            get { return envents; }
            set { envents = value;
            list.Add(envents.GenerateString());
            }
        }
        //            //groupingView = {groupField=[];groupOrder=[]; groupText=[];groupColumnShow=[];groupSummary=[]; showSummaryOnHide= false; sortitems=[]; sortnames=[]; summary=[];summaryval=[]; plusicon= "ui-icon-circlesmall-plus"; minusicon= "ui-icon-circlesmall-minus"; displayField= []};


        private bool ignoreCase = false;

        public bool IgnoreCase
        {
            get { return ignoreCase; }
            set { ignoreCase = value;
            list.Add(IsBool("ignoreCase", ignoreCase));
            }
        }
        private string cmTemplate;

        public string CmTemplate
        {
            get { return cmTemplate; }
            set { cmTemplate = value;
            list.Add(IsStringNull("cmTemplate", cmTemplate));
            }
        }
        private string idPrefix;

        public string IdPrefix
        {
            get { return idPrefix; }
            set { idPrefix = value;
            list.Add(IsStringNull("idPrefix", idPrefix));
            }
        }


        public override string GenerateString()
        {
            list.Add(IsStringNull("datatype", datatype.ToString()));
            list.Add(IsStringNull("mtype", mtype.ToString()));
            
            return string.Join(","+Environment.NewLine,list.ToArray());
        }

        private string GetColNames(){
            StringBuilder builder = new StringBuilder();
            if (colNames != null){
                builder.Append("colNames:[");
                List<string> list = new List<string>();
                foreach (var item in colNames)
                {
                    list.Add("\""+item+"\"");
                }
                builder.Append(string.Join(",",list.ToArray()));
                builder.Append("]");
            }
            return builder.ToString();
        }

        /// <summary>
        /// 生成列配置
        /// </summary>
        private string GetColModel() 
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("colModel: [");
            List<string> list = new List<string>();
            if (colModel!=null) 
            {
                foreach (var item in colModel)
                {
                    list.Add(item.GenerateString());
                }
            }
            if (list.Count > 0) 
            {
                builder.Append(String.Join(","+Environment.NewLine, list.ToArray()));
            }
            builder.Append("]");

            return builder.ToString();
        }


    }
}
